package com.kucan.recyclecard.base;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.kucan.recyclecard.R;
import com.kucan.recyclecard.application.MyApplication;
import com.kucan.recyclecard.core.AppAction;
import com.kucan.recyclecard.eventbus.EventCenter;
import com.kucan.recyclecard.eventbus.EventConstants;
import com.kucan.recyclecard.utils.DataUtils;
import com.kucan.recyclecard.utils.RouteUtil;
import com.kucan.recyclecard.utils.ToolbarUtils;
import com.kucan.recyclecard.view.LoadingDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


/**
 * Created by Administrator on 2017/5/3 0003.
 */

public abstract class BaseActivity extends AppCompatActivity {
    protected Context mContext;
    public MyApplication myApplication;
    public AppAction mAppAction;
    private Dialog mLoadingDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ToolbarUtils.setStatusBar(this, false, true);
        ToolbarUtils.setStatusTextColor(true, this);
        mContext = this;
        setContentView(setContentView());
        findViews();
        setListeners();
        myApplication = MyApplication.getApplication();
        mAppAction = myApplication.getAppAction();
        //eventbus注册
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @SuppressWarnings("unchecked")
    public <T extends View> T $(int id) {
        return (T) super.findViewById(id);
    }

    @SuppressWarnings("unchecked")
    public <T extends View> T $(int id, View.OnClickListener listener) {
        T t = (T) super.findViewById(id);
        t.setOnClickListener(listener);
        return t;
    }


    /**
     * 加载子类布局
     */
    protected abstract int setContentView();

    /**
     * 加载控件
     */
    protected abstract void findViews();

    /**
     * 设置监听
     */
    protected abstract void setListeners();


    /**
     * 接收事件
     *
     * @param eventCenter
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(EventCenter eventCenter) {
        if (null != eventCenter) {
            onEventComming(eventCenter);
        }
    }

    /**
     * 接收粘性事件
     * @param eventCenter
     */
    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void onEventStickyMainThread(EventCenter eventCenter) {
        if (null != eventCenter) {
            onEventComming(eventCenter);
        }
    }

    /**
     * 监听具体事件
     *
     * @param eventCenter
     */

// 接收粘性事件的方法
    protected void onEventComming(EventCenter eventCenter) {
        int eventCode = eventCenter.getEventCode();
        switch (eventCode) {
            case EventConstants.EVENTCODE_CLOSE_ALL_ACTIVITY://关闭所有activity
                finish();
                break;
            case EventConstants.EVENTCODE_TOKEN_ERRO://token过期去登录页面
                isUserValidLogin();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        //取消注册
        EventBus.getDefault().unregister(this);
        super.onDestroy();
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.dismiss();
            mLoadingDialog = null;
        }
    }


    /**
     * 未登录或登录过期状态需要做出提示时调用此方法
     */
    public boolean isUserValidLogin() {
//        if (DataUtils.getUserId(this).equals("0") || !DataUtils.isLogin(this)) {
//            onLogoutStatus();
//            return false;
//        }
        return true;
    }

    /**
     * 根据状态跳转页面
     */
    public void onLogoutStatus() {
//        if (DataUtils.isRegister(this)) {
//            RouteUtil.toLoginRegisterActivity(this, LoginRegisterActivity.TYPE_LOGIN);
//        } else {
//            RouteUtil.toLoginRegisterActivity(this, LoginRegisterActivity.TYPE_REGISTER);
//        }
    }

    /**
     * 展示loading
     */
    public void loading() {
        if (mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog(this);
            mLoadingDialog.show();
        } else if (!mLoadingDialog.isShowing()) {
            mLoadingDialog.show();
        }
    }

    /**
     * 关闭loading
     */
    public void cancelLoading() {
        if (null != mLoadingDialog && mLoadingDialog.isShowing()) {
            mLoadingDialog.cancel();
        }
    }

    /**
     * 监听系统返回
     *
     * @param keyCode The value in event.getKeyCode().
     * @param event   Description of the key event.
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            backActivity();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 退出到某个Activity
     */
    public void backActivity() {
        overridePendingTransition(R.anim.not_exit_push_left_in, R.anim.push_right_out);
        finish();
    }
}
